package com.filetransfer.view.Components;
import com.filetransfer.api.generated.models.ServiceInfo;
import com.filetransfer.api.generated.models.ServiceInfo5Day;
import lombok.extern.slf4j.Slf4j;
import net.miginfocom.swing.MigLayout;
import raven.chart.bar.HorizontalBarChart;
import raven.chart.data.category.DefaultCategoryDataset;
import raven.chart.data.pie.DefaultPieDataset;
import raven.chart.line.LineChart;

import javax.swing.*;
import javax.swing.border.AbstractBorder;
import java.awt.*;
@Slf4j
public class TaskChartPanel extends JPanel {
    private final LineChart lineChart;
    private final HorizontalBarChart barChart;
    public TaskChartPanel() {
        setLayout(new MigLayout("wrap 1", "[grow]", "[grow 50][grow 50]"));
        // 创建并添加 LineChart 面板
        JPanel lineChartPanel = new JPanel(new BorderLayout());
        lineChartPanel.setBorder(new RoundedBorder(15, Color.GRAY));
        lineChart = createLineChart();
        lineChartPanel.add(lineChart, BorderLayout.CENTER);
        add(lineChartPanel, "grow");
        // 创建并添加 HorizontalBarChart 面板
        JPanel barChartPanel = new JPanel(new BorderLayout());
        barChartPanel.setBorder(new RoundedBorder(15, Color.GRAY));
        barChart = createBarChart();
        barChartPanel.add(barChart, BorderLayout.CENTER);
        add(barChartPanel, "grow");
    }
    private LineChart createLineChart() {
        LineChart chart = new LineChart();
        chart.setOpaque(false);
        chart.getChartColor().addColor(Color.decode("#38bdf8"), Color.decode("#fb7185"), Color.decode("#34d399"));
        JLabel header = new JLabel("任务数据折线图");
        chart.setHeader(header);
        createLineChartData(chart);
        return chart;
    }
    private void createLineChartData(LineChart chart) {
        DefaultCategoryDataset<String, String> dataset = new DefaultCategoryDataset<>();
        // 示例数据
        dataset.addValue(100, "上传量", "昨日");
        dataset.addValue(150, "下载量", "昨日");
        dataset.addValue(120, "上传量", "今日");
        dataset.addValue(170, "下载量", "今日");
        chart.setCategoryDataset(dataset);
    }
    public void updateLineChartData(ServiceInfo5Day serviceInfo5Day) {
        DefaultCategoryDataset<String, String> dataset = new DefaultCategoryDataset<>();
        ServiceInfo[] days = {
                serviceInfo5Day.getDay1(),
                serviceInfo5Day.getDay2(),
                serviceInfo5Day.getDay3(),
                serviceInfo5Day.getDay4(),
                serviceInfo5Day.getDay5()
        };
        String[] dayLabels = {"第一天", "第二天", "第三天", "第四天", "第五天"};
        log.info("days: " + days);
        for (int i = 0; i < days.length; i++) {
            ServiceInfo day = days[i];
//            dataset.addValue(day != null ? day.uploadAmount : 0, "上传量", dayLabels[i]);
            dataset.addValue(day != null ? day.getFileInfo().getTotalDownloadCount() : 0, "下载量", dayLabels[i]);
        }
        // 更新图表数据
        lineChart.setCategoryDataset(dataset);
        lineChart.repaint();
    }
    private HorizontalBarChart createBarChart() {
        HorizontalBarChart chart = new HorizontalBarChart();
        chart.setBarColor(Color.decode("#f97316"));
        JLabel header = new JLabel("文件分布图");
        chart.setHeader(header);
        return chart;
    }
    public void updateBarChartData(DefaultPieDataset<String> dataset) {
        barChart.setDataset(dataset);
    }
    // 自定义圆角边框
    static class RoundedBorder extends AbstractBorder {
        private final int radius;
        private final Color borderColor;
        public RoundedBorder(int radius, Color borderColor) {
            this.radius = radius;
            this.borderColor = borderColor;
        }
        @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(borderColor);
            g2.drawRoundRect(x, y, width - 1, height - 1, radius, radius);
        }
        @Override
        public Insets getBorderInsets(Component c) {
            return new Insets(radius, radius, radius, radius);
        }
        @Override
        public Insets getBorderInsets(Component c, Insets insets) {
            insets.left = insets.top = insets.right = insets.bottom = radius;
            return insets;
        }
    }
}
